//C09:require.h
// Test for error conditions in programs
// Local "using namespace std" for old complilers
#ifndef REQUIRE_H_
#define REQUIRE_H_
// #include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <fstream>
#include <stdio.h>
#include <string>

#define IFOPEN(VAR, NAME)  ifstream VAR(NAME);  assure(VAR, NAME);

inline void require(bool requireemnt,
    const std::string & msg = "Requirement failed")
    {
        if(!requireemnt)
        {
            fputs(msg.c_str(), stderr);
            fputs("\n", stderr);
            exit(1);
        }
    }

inline void requireArgs(int argc, int args,
    const std::string & msg = "Must use %d arguments")
    {
        using namespace std;
        if(argc != args + 1)
        {
            fprintf(stderr, msg.c_str(), args);
            fputs("\n", stderr);
            exit(1);
        }
    }

inline void requireMinArgs(int argc, int minArgs,
    const std::string & msg = "Must use at least %d arguments")
    {
        using namespace std;
        if(argc < minArgs)
        {
            fprintf(stderr, msg.c_str(), minArgs);
            fputs("\n", stderr);
            exit(1);
        }
    }

inline void assure(std::ifstream & in,
    const std::string & filename = "")
    {
        using namespace std;
        if(!in)
        {
            fprintf(stderr, "Could not open file %s\n",
                filename.c_str());
            exit(1);    
        }
    }    
    
inline void assure(std::ofstream & out,
    const std::string & filename = "")
    {
        if(!out)
        {
            fprintf(stderr, "Could not open file %s\n",
                filename.c_str());
            exit(1);
        }
    }
#endif